package yxy.game.pm2.gm.url.mergearea;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.module.active.progress.RankProgress;
import yxy.game.pm2.module.active.progress.RankProgressArea;

public class MergeAreaHandle_排行进度 implements MergeAreaHandle {

	@Override
	public void handle(AreaBean newAreaBean, Collection<AreaBean> areas) {
		merge(newAreaBean, areas, RankProgressArea.ConquestRank);
		merge(newAreaBean, areas, RankProgressArea.TowerRank);
		merge(newAreaBean, areas, RankProgressArea.PowerRank);
	}

	private void merge(AreaBean newAreaBean, Collection<AreaBean> areas, int type) {
		HashMap<Integer, RankProgress> pMap = new HashMap<>();
		for (AreaBean areaBean : areas) {
			RankProgressArea rpa = RankProgressArea.get(areaBean, type);
			Set<Entry<Integer, RankProgress>> entrySet = rpa.getRpmap().entrySet();
			for (Entry<Integer, RankProgress> entry : entrySet) {
				int key = entry.getKey();
				RankProgress rp = entry.getValue();
				if (!pMap.containsKey(key)) {
					pMap.put(key, rp);
					continue;
				}
				RankProgress prp = pMap.get(key);
				if (rp.getTime().isBefore(prp.getTime())) {
					pMap.put(key, rp);
				}
			}
		}
		RankProgressArea rpa = RankProgressArea.get(newAreaBean, type);
		rpa.setRpmap(pMap);
		rpa.set();
	}

}
